package cn.ac.aircas.system.service.impl;

import cn.ac.aircas.system.domain.vo.RoadConditionPointVO;
import cn.ac.aircas.system.domain.vo.RoadConditionTabvo;
import cn.ac.aircas.system.mapper.RoadServiceMapper;
import cn.ac.aircas.system.mapper.SysRegionMapper;
import cn.ac.aircas.system.service.IRoadService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ac.aircas.system.domain.RoadConditionTab;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Service
public class RoadServiceImpl implements IRoadService {
    @Autowired
    private RoadServiceMapper roadServiceMapper;

    @Autowired
    private SysRegionMapper sysRegionMapper;

    @Override
    public int updateRoadConditionStatus(RoadConditionTab roadConditionTab) {
        return roadServiceMapper.updateRoadConditionStatus(roadConditionTab);
    }

    @Override
    public List<RoadConditionTab> selectRoadConditionList(RoadConditionTab roadConditionTab) {
        return roadServiceMapper.selectRoadConditionList(roadConditionTab);
    }

    @Override
    public RoadConditionTab getRoad(Long conditionId) {
        return roadServiceMapper.selectRoadById(conditionId);
    }

    @Override
    public int deleteRoadByIds(Long[] conditionIds) {
        return roadServiceMapper.deleteRoadByIds(conditionIds);
    }

    @Override
    public List<RoadConditionTabvo> getRoadList(Map param) {
        String tabName = "road_condition_tab";
        if (param != null && param.get("cityName") != null) {
            tabName = "road_condition_tab_" + param.get("cityName").toString();
        }
        param.put("tabName", tabName);
        return roadServiceMapper.selectRoadList(param);
    }

    @Override
    public List<RoadConditionPointVO> getRoadPointList(Map param) {
        String tabName = "road_condition_point";
        if (param != null && param.get("cityName") != null) {
            tabName = "road_condition_point_" + param.get("cityName").toString();
        }
        param.put("tabName", tabName);
        return roadServiceMapper.getRoadPointList(param);
    }

    @Override
    public Map<String, BigDecimal> statRoadConditions(String cityName) {
        String tabName = "road_condition_tab";
        if (cityName!= null &&!cityName.isEmpty()) {
            tabName = "road_condition_tab_" + cityName;
        }
        Map<String, BigDecimal> stringBigDecimalMap = roadServiceMapper.statRoadConditionClass(tabName);
        Map<String,BigDecimal> result = new LinkedHashMap<>();
        if (stringBigDecimalMap != null) {
            for (Map.Entry entry : stringBigDecimalMap.entrySet()) {
                result.put((String) entry.getKey(),  BigDecimal.valueOf(Double.parseDouble(entry.getValue().toString())).divide(BigDecimal.valueOf(1000),2, RoundingMode.HALF_UP));
            }
        }
        return result;
    }

    @Override
    public Map getRoadConditionStat(String cityName) {
        String tabName = "road_condition_tab";
        if (cityName!= null &&!cityName.isEmpty()) {
            tabName = "road_condition_tab_" + cityName;
        }
        Map<String, BigDecimal> map = roadServiceMapper.selectRoadConditionStat(tabName);
        Map<String,BigDecimal> res = new LinkedHashMap<>();

        for (Map.Entry entry : map.entrySet()) {
            res.put((String) entry.getKey(),  BigDecimal.valueOf(Double.parseDouble(entry.getValue().toString())).divide(BigDecimal.valueOf(1000),2, RoundingMode.HALF_UP));
        }
        return res;
    }

    @Override
    public List<Map> getRoadAbnormalStat(String cityName) {
        String tabName = "road_condition_tab";
        if (cityName!= null &&!cityName.isEmpty()) {
            tabName = "road_condition_tab_" + cityName;
        }
        LinkedList<Map> init = new LinkedList<>();
        List<Map> list = roadServiceMapper.selectRoadAbnormalStat(tabName);
        init.addAll(list);

        List<String> regioned = new ArrayList<>();
        list.forEach(item-> {
            regioned.add(item.get("region").toString());
        });

        List<String> districts = sysRegionMapper.getDistrictsByCityName(cityName);

        for (String region : districts) {
            if (!regioned.contains(region)) {
                Map<String, Object> map = new HashMap<String, Object>() {{
                    put("region", region);
                    put("abnormalLen", 0.00);
                    put("testedLen",0.00);
                }};
                init.add(map);
            }
        }

        return init;
    }
}
